package com.lx.javasunmmrize.factory.simpleFactory.order;

import com.lx.javasunmmrize.factory.simpleFactory.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Description TODO
 * @Author lx
 * @Date 2021/2/28 18:05
 **/
public class OrderPizza {

    //定义一个简单工厂
    SimpleFactory simpleFactory;

    public OrderPizza(SimpleFactory simpleFactory){
        setFactory(simpleFactory);
    }


    public Pizza setFactory(SimpleFactory simpleFactory){

        //用户输入的类型
        String orderType = "";
        Pizza pizza = null;

        this.simpleFactory = simpleFactory;//设置简单工厂对象

        do {
            orderType = getOrderType();//用户输入的方法

            pizza = this.simpleFactory.createPizza(orderType);
            if (pizza!=null){//订购成功
                pizza.bake();
                pizza.cut();
                pizza.box();
                pizza.prepare();
            }else {
                System.out.println("订购失败");

                break;//退出do-while
            }
        }while (true);

        return pizza;
    }

    public String getOrderType() {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("输入的pizza种类：");
        try {
            String str = reader.readLine();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }

    }
}
